package dao;

import com.sun.corba.se.pept.transport.ConnectionCache;
import common.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

//这个类封装了针对problem的增删改查
//1.新增题目
//2.删除题目
//3.查询题目列表
//4.查询题目详情
public class ProblemDAO {
    public void insert(Problem problem)  {
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            //1.和数据库建立连接
           connection= DBUtil.getConnection();
            //2.构造sql语句
            String sql="insert into oj_table values(null,?,?,?,?,?)";
             statement=connection.prepareStatement(sql);
            statement.setString(1,problem.getTitle());
            statement.setString(2,problem.getLevel());
            statement.setString(3,problem.getDescription());
            statement.setString(4,problem.getTemplateCode());
            statement.setString(5,problem.getTestCode());
            //3.执行sql
            int ret=statement.executeUpdate();
            if(ret!=1){
                System.out.println("新增失败");
            }else{
                System.out.println("新增成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection, (com.mysql.jdbc.PreparedStatement) statement,null);
        }
    }
    public  void delete(int id){
        Connection connection=null;
        PreparedStatement statement=null;
            try {
                connection = DBUtil.getConnection();
                String sql = "delete from oj_table where id = ?";
                statement = connection.prepareStatement(sql);
                statement.setInt(1, id);
                 int ret=statement.executeUpdate();
                 if(ret!=1){
                     System.out.println("删除失败");
                 }else{
                     System.out.println("删除成功");
                 }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                DBUtil.close(connection, (com.mysql.jdbc.PreparedStatement) statement,null);
            }
    }

    //把当前所有的题目列表中的所有题都查出来
    //万一数据库中题目很多，实现分页查找。后台容易实现分页查询
    //前端传过来一个当前的“页码”，根据页码算一下，语句sql limit offset 语句，就要算出来offset是几
    //但是前端这里实现一个分页器比较麻烦（相较后端），暂时不实现分页功能。
    public List<Problem> selectAll(){
        List<Problem>problems=new ArrayList<>();
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            //和数据库建立连接
            connection=DBUtil.getConnection();
            //拼接SQL
            String sql="select id, title, level from oj_table";
            statement=connection.prepareStatement(sql);
            //执行SQL
             resultSet=statement.executeQuery();
            //遍历resultSet
            while(resultSet.next()){
                //每一行都是一个Problem对象
                Problem problem=new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problems.add(problem);
            }
            return problems;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection, (com.mysql.jdbc.PreparedStatement) statement,resultSet);
        }
        return null;
    }

    public Problem selectOne(int id ){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            //和数据库建立连接
            connection=DBUtil.getConnection();
            //拼接sql语句
            String sql="select * from oj_table where id = ?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            //执行sql
            resultSet=statement.executeQuery();
            //遍历结果查询（由于id是逐渐，按照id查找的结果一定是唯一的）
            if(resultSet.next()){
                Problem problem=new Problem();
                problem.setId(resultSet.getInt("id"));
                problem.setTitle(resultSet.getString("title"));
                problem.setLevel(resultSet.getString("level"));
                problem.setDescription(resultSet.getString("description"));
                problem.setTemplateCode(resultSet.getString("templateCode"));
                problem.setTestCode(resultSet.getString("testCode"));
                return problem;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(connection, (com.mysql.jdbc.PreparedStatement) statement,resultSet);
        }
        return null;
    }
    public  static void testInsert(){
        ProblemDAO problemDAO=new ProblemDAO();
        Problem problem=new Problem();
        //problem.setId();
        problem.setTitle("两数之和");
        problem.setLevel("简单");
        problem.setDescription("给定一个整数数组 nums和一个整数目标值 target，请你在该数组中找出 和为目标值 target 的那两个整数，并返回它们的数组下标。\n" +
                "\n" +
                "你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。\n" +
                "\n" +
                "你可以按任意顺序返回答案。\n" +
                "\n" +
                " \n" +
                "\n" +
                "示例 1：\n" +
                "\n" +
                "输入：nums = [2,7,11,15], target = 9\n" +
                "输出：[0,1]\n" +
                "解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。\n" +
                "示例 2：\n" +
                "\n" +
                "输入：nums = [3,2,4], target = 6\n" +
                "输出：[1,2]\n" +
                "示例 3：\n" +
                "\n" +
                "输入：nums = [3,3], target = 6\n" +
                "输出：[0,1]\n" +
                "\n" +
                "\n" +
                "提示：\n" +
                "\n" +
                "2 <= nums.length <= 104\n" +
                "-109 <= nums[i] <= 109\n" +
                "-109 <= target <= 109\n" +
                "只会存在一个有效答案\n" +
                "进阶：你可以想出一个时间复杂度小于 O(n2) 的算法吗？\n" +
                "\n"  );


        problem.setTemplateCode("class Solution {\n" +
                "    public int[] twoSum(int[] nums, int target) {\n" +
                "\n" +
                "    }\n" +
                "}");
//        problem.setTestCode("public static void main(String[] args) {\n" +
//                "        Solution solution = new Solution();\n" +
//                "        //test_case1\n" +
//                "        int nums[] = {2,7,11,15};\n" +
//                "        int target = 9;\n" +
//                "        int[] result = solution.twoSum(nums,target);\n" +
//                "        if (result.length == 2 && result[0] == 0 && result[1] == 1) {\n" +
//                "            System.out.println(\"test_case1 OK\");\n" +
//                "        } else {\n" +
//                "            System.out.println(\"test_case1 Failed\");\n" +
//                "        }\n" +
//                "        \n" +
//                "        //test_case2\n" +
//                "        int nums2[] = {3,2,4};\n" +
//                "        int target2 = 6;\n" +
//                "        int[] result2 = solution.twoSum(nums2,target2);\n" +
//                "        if (result2.length == 2 && result[0] == 1 && result[1] == 2) {\n" +
//                "            System.out.println(\"test_case2 OK\");\n" +
//                "        } else {\n" +
//                "            System.out.println(\"test_case2 Failed\");\n" +
//                "        }\n" +
//                "    }");
//        problem.setTemplateCode();
        problemDAO.insert(problem);
        System.out.println("插入成功");
    }

    private static void testSelectAll() {
        ProblemDAO problemDAO=new ProblemDAO();
        List<Problem> problems= problemDAO.selectAll();
        System.out.println(problems);
    }
    private static void testSelectOne(){
        ProblemDAO problemDAO=new ProblemDAO();
        Problem problem=problemDAO.selectOne(1);
        System.out.println(problem);
    }
    private static void testDelete(){
        ProblemDAO problemDAO=new ProblemDAO();
        problemDAO.delete(1);
    }
    public static void main(String[] args) {
       testInsert();
        //testSelectAll();
//        testSelectOne();
//        testDelete();
    }


}
